package com.gai.web.music.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gai.web.system.entity.Message;
import com.gai.web.music.entity.GAlbum;
import com.gai.web.music.entity.GSong;
import com.gai.web.music.service.GAlbumService;
import com.gai.web.music.service.GSongService;

import com.gai.web.music.vo.SongByYearVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Captain
 * @Date: 2022/5/16
 * @Time: 10:09
 * @Description:
 */
@RestController
@Api(value="音乐",tags="音乐")
@RequestMapping("/album")
public class GAlbumController {
    @Autowired
    private GAlbumService gAlbumService;
    @Autowired
    private GSongService gSongService;
    /**
     * 专辑列表
     */
    @ApiOperation(value="分页查询",notes="分页查询")
    @GetMapping("/list")
    public Message queryPageList(@RequestParam(name = "pageNo", defaultValue = "1")Integer pageNo,
                                 @RequestParam(name = "pageSize", defaultValue = "5")Integer pageSize,
                                 @RequestParam(name = "searchName",required=false)String searchName,
                                 @RequestParam(name = "startDate",required=false)String startDate,
                                 @RequestParam(name = "endDate",required=false)String endDate){
        QueryWrapper<GAlbum> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(searchName), "name", searchName);
        queryWrapper.ge(StringUtils.hasText(startDate), "date", startDate);
        queryWrapper.le(StringUtils.hasText(endDate), "date", endDate);
        queryWrapper.orderByDesc("date");
        Page<GAlbum> page= new Page<GAlbum>(pageNo, pageSize);
        IPage<GAlbum> pageList=gAlbumService.page(page,queryWrapper);
        return Message.success("获取成功！").add("pageList", pageList);
    }

    /**
     * 得到某一专辑信息
     */
    @ApiOperation(value="按id查专辑信息",notes="按id查专辑信息")
    @GetMapping("/get/{aid}")
    public Message queryById(@PathVariable("aid")Integer aid){
        GAlbum album=gAlbumService.getById(aid);
        if(album==null)return Message.success("获取失败");
        else {
            return Message.success("获取成功！").add("albumDetail", album);
        }
    }
    /**
     * 某专辑下的歌曲列表
     */
    @ApiOperation(value="按专辑id得歌曲列表",notes="按专辑id得歌曲列表")
    @GetMapping("/getSong")
    public Message querySongByAlbum(@RequestParam(name = "aid")Integer aid){
        QueryWrapper<GSong> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid",aid);
        List<GSong> songList=gSongService.list(queryWrapper);
        return Message.success("获取成功！").add("songList", songList);
    }
    /**
     * 专辑总数 歌曲总数
     */
    @ApiOperation(value="专辑总数 歌曲总数",notes="专辑总数 歌曲总数")
    @GetMapping("/getCount")
    public Message queryCount(){
        QueryWrapper<GSong> queryWrapper = new QueryWrapper<>();
        int song_num=gSongService.count();
        int album_num=gAlbumService.count();
        return Message.success("获取成功! ").add("song_num", song_num).add("album_num",album_num);
    }
    /**
     * 新增专辑
     */
    @ApiOperation(value="新增专辑",notes="新增专辑")
    @PostMapping("/add")
    public Message add(@RequestBody GAlbum gAlbum){
        if (gAlbumService.save(gAlbum)) {
            return Message.success("新增成功!");
        } else {
            return Message.error("新增失败!");
        }
    }
    /**
     * 编辑专辑信息
     */
    @ApiOperation(value="编辑专辑",notes="编辑专辑")
    @PostMapping("/edit")
    public Message editAlbum(@RequestBody GAlbum gAlbum){
        if(gAlbumService.updateById(gAlbum)){
            return Message.success("更新成功！");
        }else{
            return Message.error("更新失败！");
        }
    }
    /**
     * 删除专辑信息
     */
    @ApiOperation(value="删除专辑",notes="删除专辑")
    @DeleteMapping("/delete")
    public Message deleteAlbum(@RequestParam(name = "id")Integer id){
        if (gAlbumService.removeById(id)) {
            return Message.success("删除成功");
        }else {
            return Message.error("该专辑下存在歌曲，无法删除");
        }
    }
    /**
     * 新增歌曲
     */
    @ApiOperation(value="新增歌曲",notes="新增歌曲")
    @PostMapping("/addSong")
    public Message addSong(@RequestBody GSong gSong){
        if (gSongService.save(gSong)) {
            return Message.success("新增成功!");
        } else {
            return Message.error("新增失败!");
        }
    }
    /**
     * 编辑歌曲信息
     */
    @ApiOperation(value="编辑歌曲",notes="编辑歌曲")
    @PostMapping("/editSong")
    public Message editSong(@RequestBody GSong gSong){
        if(gSongService.updateById(gSong)){
            return Message.success("更新成功！");
        }else{
            return Message.error("更新失败！");
        }
    }
    /**
     * 删除歌曲信息
     */
    @ApiOperation(value="删除歌曲",notes="删除歌曲")
    @DeleteMapping("/deleteSong")
    public Message deleteSong(@RequestParam(name = "id")Integer id){
        if (gSongService.removeById(id)) {
            return Message.success("删除成功");
        }else {
            return Message.error("删除失败");
        }
    }
    /**
     * 按年专辑歌曲列表
     */
    @ApiOperation(value="统计每年发歌数量",notes="统计每年发歌数量")
    @GetMapping("/getSongByYear")
    public Message getSongByYear(){
        List<SongByYearVo> songByYear = gSongService.getSongByYear();
        if(songByYear.size()<1){
            return Message.error("获取失败！");
        }else{
            List yearList=songByYear.stream().map(SongByYearVo::getYear).collect(Collectors.toList());
            List numList=songByYear.stream().map(SongByYearVo::getNum).collect(Collectors.toList());
            return Message.success("获取成功！").add("yearList", yearList).add("numList", numList);
//            return Message.success("获取成功！").add("data", songByYear);
        }
    }
}
